﻿using Cysharp.Threading.Tasks;
using Logic;
using Services.Window;

namespace Infrastructure.States
{
    internal class LoadMenuState : IPayloadedState<string>
    {
        private readonly GameStateMachine _stateMachine;
        private readonly SceneLoader _sceneLoader;
        private readonly LoadingCurtain _loadingCurtain;
        private readonly IWindowService _windowService;

        public LoadMenuState(
            GameStateMachine stateMachine, 
            SceneLoader sceneLoader, 
            LoadingCurtain loadingCurtain, 
            IWindowService windowService)
        {
            _stateMachine = stateMachine;
            _sceneLoader = sceneLoader;
            _loadingCurtain = loadingCurtain;
            _windowService = windowService;
        }

        public void Enter(string sceneName)
        {
            _loadingCurtain.Show();
            _sceneLoader.Load(sceneName, OnLoaded).Forget();
        }

        public void Exit()
        {

        }

        private void OnLoaded()
        {
            _windowService.Open(WindowId.Root);
            _windowService.Open(WindowId.MenuWindow);
            _loadingCurtain.Hide().Forget();
            _stateMachine.Enter<MenuLoopState>();
        }
    }
}